package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.PanelConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.*;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelContentItemDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @auther cskaoyan
 * @date 2022/8/24:14:59
 */
@Service
public class IProductServiceImpl implements IProductService {
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    ProductConverter productConverter;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ContentConverter contentConverter;
    @Autowired
    PanelConverter panelConverter;
    @Override
    public ProductDetailDto getProductDetail(Long id) {
        Item item = itemMapper.selectByPrimaryKey(id);
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        ProductDetailDto productDetailDto = productConverter.toProductDetailDto(item, itemDesc);
        System.out.println(productDetailDto);
        return productDetailDto;
    }

    @Override
    public AllProductResponse getAllProduct(PageRequest pageInfo) {
        PageHelper.startPage(pageInfo.getPage(),pageInfo.getSize());
        Example example = new Example(Item.class);
        Example.Criteria criteria = example.createCriteria();
        String orderBy=null;
        if (pageInfo.getSort()==null){
            orderBy="created desc";
        }else if (pageInfo.getSort().equals("1")){
            orderBy="price asc";
        }else if (pageInfo.getSort().equals("-1")){
            orderBy="price desc";
        }
        example.setOrderByClause(orderBy);
        if (pageInfo.getPriceGt() != null && !"".equals(pageInfo.getPriceGt())) {
            criteria.andGreaterThanOrEqualTo("price",pageInfo.getPriceGt());
        }
        if (pageInfo.getPriceLte() != null && !"".equals(pageInfo.getPriceLte())) {
            criteria.andLessThanOrEqualTo("price",pageInfo.getPriceLte());
        }
        if (pageInfo.getCid() != null && !"".equals(pageInfo.getCid())) {
            criteria.andEqualTo("cid",pageInfo.getCid());
        }
        List<Item> items = itemMapper.selectByExample(example);
        PageInfo<Item> itemPageInfo = new PageInfo<>(items);
        List<ProductDto> productDtos = productConverter.items2Dto(items);
        AllProductResponse allProductResponse = new AllProductResponse();
        allProductResponse.setTotal(itemPageInfo.getTotal());
        allProductResponse.setData(productDtos);
        return allProductResponse;
    }

    @Override
    public Set<PanelDto> getRecommendGoods() {
        Example example = new Example(Panel.class);
        example.createCriteria().andEqualTo("name","热门推荐");
        List<Panel> panels = panelMapper.selectByExample(example);
        Panel panel = panels.get(0);
        Example example1 = new Example(PanelContent.class);
        example1.createCriteria().andEqualTo("panelId",panel.getId());
        List<PanelContent> panelContents = panelContentMapper.selectByExample(example1);
        ArrayList<PanelContentItemDto> panelContentItemDtos1 = new ArrayList<>();
        for (PanelContent panelContent : panelContents) {
            Item item = itemMapper.selectByPrimaryKey(panelContent.getProductId());
            PanelContentItemDto panelContentItemDto = panelConverter.PanelToPanelContentItemDto(panelContent, item);
            panelContentItemDtos1.add(panelContentItemDto);
        }

        PanelDto panelDto = panelConverter.PanelAndList(panel, panelContentItemDtos1);
        Set<PanelDto> panelDtos = new HashSet<>();
        panelDtos.add(panelDto);
        return panelDtos;
    }

    @Override
    public List<ItemDto> getAllItems() {
        return null;
    }
}
